﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using NIIT.SaoBacDau.Aupi.HospitalManagement.BusinessLogic;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Languages;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement
{
    public partial class PatientHistory : Form
    {
        private List<Pa_DetailsMedical> pa_Details;
        private List<F_TypeMedical> f_TypeMedical;
        private List<Pa_Patient> pa_Patient;
        private List<D_OrganizationDetails> d_OrgDetails;
        private List<Pa_Patient> Load_Patient(int value, int to)
        {
            this.pa_Patient= BL_Pa_Patients.ListPatient(value, to);
            return pa_Patient;
        }

        private List<Pa_DetailsMedical> Load_Pa_DetailsMedical(int value, int to)
        {
            this.pa_Details = BL_Pa_DetailsMedical.ListDetailsMedical(value, to);
            return pa_Details;
        }

        private List<F_TypeMedical> LoadDepartment()
        {
            f_TypeMedical =  BL_F_TypeMedical.ListTypeMedical();
            return f_TypeMedical;
        }

        public PatientHistory()
        {
            InitializeComponent();
        }

        private void PatientHistory_Load(object sender, EventArgs e)
        {
            cbx_ShowRow.SelectedIndex = 0;
            cbx_TypeSearch.SelectedIndex = 0;
        }

        private void LoadData()
        {
            lv_PatientMedical.Items.Clear();
            for (int i = 0; i < pa_Patient.Count; i++)
            {
                if (pa_Patient[i].PatientID == pa_Details[i].PatientID)
                {
                    for (int j = 0; j < f_TypeMedical.Count; j++)
                    {
                        if (pa_Details[i].TypeMedicalID == f_TypeMedical[j].TypeMedicalID)
                        {
                            ListViewItem lvi = new ListViewItem(pa_Details[i].DetailsMedicalID.ToString());
                            lvi.SubItems.Add(f_TypeMedical[j].NameMedical.ToString());
                            lvi.SubItems.Add(pa_Patient[i].Name.ToString());
                            lvi.SubItems.Add(pa_Patient[i].HospitalizedDate.ToString());
                            lvi.SubItems.Add("  ");
                            lvi.SubItems.Add(pa_Patient[i].TypeTreatment.ToString());
                            lv_PatientMedical.Items.Add(lvi);
                        }
                    }
                }
            }
            lbl_TotalValue.Text = pa_Details.Count.ToString();
        }

        private void cbx_ShowRow_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbx_ShowRow.SelectedIndex == 0)
                LoadIstance(0, 30);
            else if (cbx_ShowRow.SelectedIndex == 1)
                LoadIstance(30, 60);
            else if (cbx_ShowRow.SelectedIndex == 2)
                LoadIstance(60, 90);
            else if (cbx_ShowRow.SelectedIndex == 3)
                LoadIstance(90, 120);
            else if (cbx_ShowRow.SelectedIndex == 4)
                LoadIstance(120, 150);
            else if (cbx_ShowRow.SelectedIndex == 5)
                LoadIstance(150, 180);
            else if (cbx_ShowRow.SelectedIndex == 6)
                LoadIstance(180, 210);
            else if (cbx_ShowRow.SelectedIndex == 7)
                LoadIstance(210, 240);
            else if (cbx_ShowRow.SelectedIndex == 8)
                LoadIstance(240, 270);
            else if (cbx_ShowRow.SelectedIndex == 9)
                LoadIstance(270, 300);
            else
                LoadIstance(0, 30);
        }

        private void LoadIstance(int value,int to)
        {
            Load_Pa_DetailsMedical(value, to);
            LoadDepartment();
            Load_Patient(value, to);
            LoadData();
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            SearchPatientHistory();
        }

        private void SearchPatientHistory()
        {
            if (string.IsNullOrEmpty(tbx_ContentSearch.Text) == true)
                MessageBox.Show("Nội dung tìm kiếm là rỗng !", "Lỗi thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            else
            {
                if (cbx_TypeSearch.SelectedIndex == 0)
                {
                    if (BL_Pa_DetailsMedical.CheckContentSearch(HM_TypeSearchMedicalHistory.DetailsMedical, tbx_ContentSearch.Text) == false)
                        MessageBox.Show("Nội dung " + '"' + tbx_ContentSearch.Text + '"' + " không đúng định dạng kiểu số !","Lỗi Thông Tin Tìm Kiếm",MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    else
                    {
                        Pa_DetailsMedical pa_DetailsMedical = new Pa_DetailsMedical();
                        pa_DetailsMedical.DetailsMedicalID = Convert.ToInt32(tbx_ContentSearch.Text);
                        Pa_History(HM_TypeSearchMedicalHistory.DetailsMedical, pa_DetailsMedical);
                    }
                }
            }
        }

        private void Pa_History(HM_TypeSearchMedicalHistory hM_TypeSearchMedicalHistory, Pa_DetailsMedical pa_DetailsMedical)
        {
            Pa_DetailsMedical pa_Medical = BL_Pa_DetailsMedical.SearchDetailsMedical(pa_DetailsMedical);
            if (pa_Medical == null)
            {
                MessageBox.Show("Không tìm thấy bệnh nhân có mã bệnh án là : " + pa_DetailsMedical.DetailsMedicalID.ToString(), "Không tìm thây", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
            else
            {
                lv_PatientMedical.Items.Clear();
                ListViewItem lvi = new ListViewItem(pa_Medical.DetailsMedicalID.ToString());
                F_TypeMedical f_Type = new F_TypeMedical();
                f_Type.TypeMedicalID = pa_Medical.TypeMedicalID;
                lvi.SubItems.Add(BL_F_TypeMedical.TypeMedical(f_Type).NameMedical);
                Pa_Patient pa_Patient = BL_Pa_Patients.pa_Patient(pa_Medical);
                lvi.SubItems.Add(pa_Patient.Name);
                lvi.SubItems.Add(pa_Patient.HospitalizedDate);
                D_OrganizationDetails d_OrgDetails = new D_OrganizationDetails();
                d_OrgDetails.OrganizationDetailsID = pa_Patient.OrganizationDetailsID;
                d_OrgDetails = BL_D_OrganizationDetailss.ObjOrganizationDetails(d_OrgDetails);
                lvi.SubItems.Add(d_OrgDetails.NameDetails);
                lvi.SubItems.Add(pa_Patient.TypeTreatment.ToString());
                lv_PatientMedical.Items.Add(lvi);
            }
        }

        private void tbx_ContentSearch_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
                SearchPatientHistory();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn muốn đóng danh sách lịch sử bệnh án bệnh nhân ?", "Đóng", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                ((Managements)this.MdiParent).DisableControl(HM_ReceptionAndManagementPatient.EnableElement);
                ((Managements)this.MdiParent).Clear();
            }
        }

        private void btn_EditPatientHistory_Click(object sender, EventArgs e)
        {
            if (lv_PatientMedical.SelectedItems.Count == 0)
                LibraryLanguages.ErrorSelect("chỉnh sửa bệnh án");
            else
            {
                MedicalHistory medical = new MedicalHistory();
                //((Managements)this.MdiParent).EditPatient(patient.PatientID);
            }
        }
    }
}
